import {defineStore} from 'pinia'
import { displayByName } from "@/cesiumTools/effectController";
export const useLineData=defineStore('lineData',{
    state:()=>{
        return {
            lineData:[],
            // 全局共享viewer，注意标记为markRaw
            viewer:null,
            tileset:null,
            lineEnts:[],
            viewer:null,
            tileset:null
        }
    },
    getters:{
        // 获取全部信息
        allData(){
            return this.lineData
        },
        Viewer(){
            return this.viewer
        },
        Tile(){
            return this.tileset
        },
        allLineEnts(){
            return this.lineEnts
        }
    },
    actions:{
        setData(data){
            return new Promise((resolve,reject)=>{
                this.lineData=data.length?data:[]
                resolve(data)
            })
        },
        setViewer(viewer){
            return new Promise((resolve,reject)=>{
                this.viewer=viewer
                resolve(viewer)
            })
        },
        setTileset(tileset){
            return new Promise((resolve,reject)=>{
                this.tileset=tileset
                resolve(tileset)    
            })
        },
        setLineEnts(lineEnts){
            return new Promise((resolve,reject)=>{
                this.lineEnts=lineEnts
                resolve(lineEnts)
            })
        },
        // 全局管理路线显隐
    displayLine(lineNames, isShow) {
        if (Array.isArray(lineNames) && this.lineData.length && !this.disable) {
          const stationNames = [];
          // 控制state数据
          this.lineData.forEach((item) => {
            const { stationsList, name } = item;
            if (lineNames.includes(name)) {
              item.checked = isShow;
              stationsList.forEach((station) => {
                stationNames.push(station.name);
              });
            }
          });
          // 控制地图元素
          displayByName(lineNames, stationNames, isShow);
        }
      },
      // 设置是否开启全局管理
      disableController(val) {
        this.disable = val;
      }
    }
})
export const useMeasureData=defineStore('measureData',{
    state:()=>{
        return {
            measureData:[]
        }
    },
    getters:{
        // 获取全部信息
        allData(){
            return this.measureData
        }
    },
    actions:{
        setData(data){
            return new Promise((resolve,reject)=>{
                this.measureData=data.length?data:[]
                resolve(data)
            })
        },
        clearData(){
          return new Promise((resolve,reject)=>{
            const data=this.measureData.map(item=>{
              item.length && item.forEach(n=>n.measures.length=0)
              return item
            })
            this.measureData=data
            resolve(data)
          })
        }
    },
  })
// 由于lineData的state是异步的，所以需要监听action来获取数据
export const watchLineData=(actionName='setData')=>{
    return new Promise((resolve,reject)=>{
        useLineData().$onAction(({name,store,args,after,onError})=>{
            if(name===actionName){
                after((res)=>{
                    console.log(res);
                    if(res){
                        resolve(res)
                    }
                })

                onError((e)=>{
                    reject(e)
                })
            }
        })
    })
}